//
//  SunCardsCell.m
//  Xspcf
//
//  Created by sunshine on 16/10/24.
//  Copyright © 2016年 小小算盘. All rights reserved.
//

#import "SunCardsCell.h"

@interface SunCardsCell()

@property (nonatomic, assign) BOOL usePage;
@property (nonatomic, assign) BOOL isAble;

@end

@implementation SunCardsCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

+ (instancetype)cellWithTableView:(UITableView *)tableView{
    static NSString *ID = @"cellID";
    SunCardsCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[SunCardsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        [cell creatSubviews];
    }
    return cell;
}

- (void)creatSubviews{
    self.backImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"card_ondate"]];
    [self.backImage setFrame:CGRectMake(10, 0, SCREENWIDTH - 20, 125)];
    [self addSubview:self.backImage];
    self.backgroundColor = [UIColor clearColor];
    // 优惠券类型
    _title = [[UILabel alloc]init];
    [self addSubview:_title];
    [_title setFrame:CGRectMake(X(26), 65, X(50), 17)];
    _title.textColor = [UIColor colorWithHexString:@"7D7D7D"];
    _title.font = [UIFont fontWithName:@"PingFangSC-Regular" size:12];
    // 优惠金额
    _content = [[UILabel alloc]init];
    [self addSubview:_content];
    [_content setFrame:CGRectMake(X(24), 4, X(120), 67)];
    _content.textColor = [UIColor colorWithHexString:@"FA7060"];
    _content.font = [UIFont fontWithName:@"PingFangSC-Medium" size:48];
    // 投资金额限制
    _start = [[UILabel alloc]init];
    [self addSubview:_start];
    [_start setFrame:CGRectMake(X(147), 15, X(200), 17)];
    _start.textColor = [UIColor colorWithHexString:@"7D7D7D"];
    _start.font = [UIFont systemFontOfSize:12];
    // 时间限制
    _limit = [[UILabel alloc]init];
    [self addSubview:_limit];
    [_limit setFrame:CGRectMake(X(147), 37, X(200), 17)];
    _limit.textColor = [UIColor colorWithHexString:@"7D7D7D"];
    _limit.font = [UIFont systemFontOfSize:12];
    
    // 卡券来源
    _from = [[UILabel alloc]init];
    [self addSubview:_from];
    [_from setFrame:CGRectMake(SCREENWIDTH - X(200) - X(40), 105, X(200), 17)];
    _from.textAlignment = NSTextAlignmentRight;
    _from.textColor = [UIColor colorWithHexString:@"7D7D7D"];
    _from.font = [UIFont systemFontOfSize:12];
    
    // 有效期
    _time = [[UILabel alloc]init];
    [self addSubview:_time];
    [_time lt_setLeft:X(26) to:nil save:YES];
    [_time lt_setTop:105 to:nil save:YES];
    _time.textColor = [UIColor colorWithHexString:@"7D7D7D"];
    _time.font = [UIFont systemFontOfSize:12];
    
    // 倒计时
    _count = [[UILabel alloc]init];
    [self addSubview:_count];
    [_count lt_setLeft:5 to:_time save:YES];
    [_count lt_setTop:105 to:nil save:YES];
    _count.textColor = [UIColor colorWithHexString:@"D0021B"];
    _count.font = [UIFont systemFontOfSize:12];
    _count.hidden = YES;
    
    _unable = [[UIImageView alloc]init];
    [self addSubview:_unable];
    [_unable lt_setSize:CGSizeMake(X(80), Y(55)) save:YES];
    [_unable lt_setTop:Y(40) to:nil save:YES];
    [_unable lt_setRight:X(28) to:nil save:YES];
    
    self.uselessImage = [[UIView alloc]init];
    [self.uselessImage setBackgroundColor:[UIColor colorWithHexString:@"F0F0F0"]];
    self.uselessLabel = [[UILabel alloc]init];
    self.uselessImage.hidden = YES;
    [self addSubview:self.uselessImage];
    [self.uselessImage addSubview:self.uselessLabel];
    [self.uselessImage setFrame:CGRectMake(17, 125, SCREENWIDTH - 34, 50)];
    self.reasonLabel = [[UILabel alloc]init];
    [self.uselessImage addSubview:self.reasonLabel];
    self.uselessLabel.text = @"不可用原因";
    self.uselessLabel.textColor = [UIColor colorWithHexString:@"D0021B"];
    self.uselessLabel.font = [UIFont systemFontOfSize:12];
    [self.uselessLabel setFrame:CGRectMake(13, 6, 70, 17)];
    self.reasonLabel.textColor = [UIColor colorWithHexString:@"8F8F8F"];
    self.reasonLabel.font = [UIFont systemFontOfSize:10];
    [self.reasonLabel setFrame:CGRectMake(13, 28, 200, 14)];
}
- (void)setCellData:(NSDictionary *)data isUse:(BOOL)use isAble:(BOOL)able{
    self.usePage = NO;
    _title.text = [data objectForKey:@"title"];
    if ([[data objectForKey:@"type"]intValue] == 1) {//红包
        [_content setMarkup:[NSString stringWithFormat:@"<font size=\"48\">%d</font><font size=\"18\">元</font>",[[data objectForKey:@"money"]intValue]]];
        int money = [[data objectForKey:@"limitMoney"]intValue];
//        if (money >= 10000) {
//            _start.text = money == 0?@"无限制":[NSString stringWithFormat:@"单笔投资满%d万元返现%d元",money/10000,[[data objectForKey:@"money"]intValue]];
//        }else{
            _start.text = money == 0?@"无限制":[NSString stringWithFormat:@"单笔投资满%d元返现%d元",money,[[data objectForKey:@"money"]intValue]];
//        }
    }else {
        [_content setMarkup:[NSString stringWithFormat:@"<font size=\"48\">%.1f</font><font size=\"24\">%%</font>",[[data objectForKey:@"apr"]doubleValue]]];
        int money = [[data objectForKey:@"limitMoney"]intValue];
//        if (money >= 10000) {
//            _start.text = money == 0?@"无限制":[NSString stringWithFormat:@"单笔投资满%d万元可用",money/10000];
//        }else{
            _start.text = money == 0?@"无限制":[NSString stringWithFormat:@"单笔投资满%d元可用",money];
//        }
    }
    _limit.text = [NSString stringWithFormat:@"%@",[data objectForKey:@"range"]];
    _time.text = [NSString stringWithFormat:@"%@ - %@",[Sun timestamp:[[data objectForKey:@"startTime"] intValue] ToTime:@"YYYY/MM/dd"],[Sun timestamp:[[data objectForKey:@"endTime"] intValue] ToTime:@"YYYY/MM/dd"]];
    NSInteger endTime = [[data objectForKey:@"endTime"] integerValue];
    NSInteger currentTime = [[self getNowTimeTimestamp2] integerValue];
    NSInteger cut = endTime - currentTime;
    if (cut>0 && cut<=60 * 60 * 24 * 2) {
        _count.hidden = NO;
        _count.text = @"仅剩1天";
    }else if (cut>60 * 60 * 24 * 2 && cut<=60 * 60 * 24 * 3){
        _count.hidden = NO;
        _count.text = @"仅剩2天";
    }else if (cut>60 * 60 * 24 * 3&& cut<=60 * 60 * 24 * 4){
        _count.hidden = NO;
        _count.text = @"仅剩3天";
    }else{
        _count.hidden = YES;
    }
    _from.text = [NSString stringWithFormat:@"%@",[data objectForKey:@"from"]];
    _unable.image = nil;
    if (!able) {
        _count.hidden = YES;
        _title.textColor = [UIColor colorWithHexString:@"D9D9D9"];
        _content.textColor = [UIColor colorWithHexString:@"D9D9D9"];
        _start.textColor = [UIColor colorWithHexString:@"D9D9D9"];
        _limit.textColor = [UIColor colorWithHexString:@"D9D9D9"];
        _time.textColor = [UIColor colorWithHexString:@"D9D9D9"];
        _from.textColor = [UIColor colorWithHexString:@"D9D9D9"];
//        if ([[data objectForKey:@"isUse"]intValue] == 1) {
//            [self.backImage setImage:[UIImage imageNamed:@"card_used"]];
//        }else {
//            [self.backImage setImage:[UIImage imageNamed:@"card_outdate"]];
//        }
    }else {
//        [self.backImage setImage:[UIImage imageNamed:@"card_ondate"]];
        _title.textColor = [HXColor colorWithHexString:@"7D7D7D"];
        _content.textColor = [UIColor colorWithHexString:@"FA7060"];
        _start.textColor = [UIColor colorWithHexString:@"7D7D7D"];
        _limit.textColor = [UIColor colorWithHexString:@"7D7D7D"];
        _time.textColor = [UIColor colorWithHexString:@"7D7D7D"];
        _from.textColor = [UIColor colorWithHexString:@"7D7D7D"];
    }
}
- (void)useCellData:(NSDictionary *)data isAble:(BOOL)able isSelect:(BOOL)isSelect{
    self.usePage = YES;
    _title.text = [data objectForKey:@"title"];
    if ([[data objectForKey:@"type"]intValue] == 1) {//红包
        [_content setMarkup:[NSString stringWithFormat:@"<font size=\"48\">%d</font><font size=\"18\">元</font>",[[data objectForKey:@"money"]intValue]]];
        int money = [[data objectForKey:@"limitMoney"]intValue];
//        if (money >= 10000) {
//            _start.text = money == 0?@"无限制":[NSString stringWithFormat:@"单笔投资满%d万元返现%d元",money/10000,[[data objectForKey:@"money"]intValue]];
//        }else{
            _start.text = money == 0?@"无限制":[NSString stringWithFormat:@"单笔投资满%d元返现%d元",money,[[data objectForKey:@"money"]intValue]];
//        }
    }else {
        [_content setMarkup:[NSString stringWithFormat:@"<font size=\"48\">%.1f</font><font size=\"24\">%%</font>",[[data objectForKey:@"apr"]doubleValue]]];
        int money = [[data objectForKey:@"limitMoney"]intValue];
//        if (money >= 10000) {
//            _start.text = money == 0?@"无限制":[NSString stringWithFormat:@"单笔投资满%d万元可用",money/10000];
//        }else{
            _start.text = money == 0?@"无限制":[NSString stringWithFormat:@"单笔投资满%d元可用",money];
//        }
    }
    _limit.text = [NSString stringWithFormat:@"%@",[data objectForKey:@"range"]];
    _time.text = [NSString stringWithFormat:@"%@ - %@",[Sun timestamp:[[data objectForKey:@"startTime"] intValue] ToTime:@"YYYY/MM/dd"],[Sun timestamp:[[data objectForKey:@"endTime"] intValue] ToTime:@"YYYY/MM/dd"]];
    NSInteger endTime = [[data objectForKey:@"endTime"] integerValue];
    NSInteger currentTime = [[self getNowTimeTimestamp2] integerValue];
    NSInteger cut = endTime - currentTime;
    if (cut>0 && cut<=60 * 60 * 24 * 2) {
        _count.hidden = NO;
        _count.text = @"仅剩1天";
    }else if (cut>60 * 60 * 24 * 2 && cut<=60 * 60 * 24 * 3){
        _count.hidden = NO;
        _count.text = @"仅剩2天";
    }else if (cut>60 * 60 * 24 * 3&& cut<=60 * 60 * 24 * 4){
        _count.hidden = NO;
        _count.text = @"仅剩3天";
    }else{
        _count.hidden = YES;
    }
    _from.text = [NSString stringWithFormat:@"%@",[data objectForKey:@"from"]];
    _unable.image = nil;
    if (!able) {
        self.isAble = NO;
        _title.textColor = [UIColor colorWithHexString:@"D9D9D9"];
        _content.textColor = [UIColor colorWithHexString:@"D9D9D9"];
        _start.textColor = [UIColor colorWithHexString:@"D9D9D9"];
        _limit.textColor = [UIColor colorWithHexString:@"D9D9D9"];
        _time.textColor = [UIColor colorWithHexString:@"D9D9D9"];
        _from.textColor = [UIColor colorWithHexString:@"D9D9D9"];
        self.uselessImage.hidden = NO;
        [self.backImage setImage:[UIImage imageNamed:@"choseCard_useless"]];

    }else {
        self.isAble = YES;
        [self.backImage setImage:[UIImage imageNamed:@"card_ondate"]];
        self.uselessImage.hidden = YES;
        _title.textColor = [HXColor colorWithHexString:@"7D7D7D"];
        _content.textColor = [UIColor colorWithHexString:@"FA7060"];
        _start.textColor = [UIColor colorWithHexString:@"7D7D7D"];
        _limit.textColor = [UIColor colorWithHexString:@"7D7D7D"];
        _time.textColor = [UIColor colorWithHexString:@"7D7D7D"];
        _from.textColor = [UIColor colorWithHexString:@"7D7D7D"];
    }
    
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    if (self.usePage) {
        if (selected == YES) {
            [self.backImage setImage:[UIImage imageNamed:@"choseCard_chose"]];
            _title.textColor = WHITE;
            _content.textColor = WHITE;
            _start.textColor = WHITE;
            _limit.textColor = WHITE;
            _time.textColor = WHITE;
            _from.textColor = WHITE;
        }else if (!self.isAble){
            _title.textColor = [UIColor colorWithHexString:@"D9D9D9"];
            _content.textColor = [UIColor colorWithHexString:@"D9D9D9"];
            _start.textColor = [UIColor colorWithHexString:@"D9D9D9"];
            _limit.textColor = [UIColor colorWithHexString:@"D9D9D9"];
            _time.textColor = [UIColor colorWithHexString:@"D9D9D9"];
            _from.textColor = [UIColor colorWithHexString:@"D9D9D9"];
            [self.backImage setImage:[UIImage imageNamed:@"choseCard_useless"]];
        }
        else {
            [self.backImage setImage:[UIImage imageNamed:@"card_ondate"]];
            _title.textColor = [HXColor colorWithHexString:@"7D7D7D"];
            _content.textColor = [UIColor colorWithHexString:@"FA7060"];
            _start.textColor = [UIColor colorWithHexString:@"7D7D7D"];
            _limit.textColor = [UIColor colorWithHexString:@"7D7D7D"];
            _time.textColor = [UIColor colorWithHexString:@"7D7D7D"];
            _from.textColor = [UIColor colorWithHexString:@"7D7D7D"];

        }
    }
    // Configure the view for the selected state
}
-(NSString *)getNowTimeTimestamp2{
    
    NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
    
    NSTimeInterval a=[dat timeIntervalSince1970];
    
    NSString *timeString = [NSString stringWithFormat:@"%0.f", a];//转为字符型

    return timeString;

}


@end
